package com.lry.web.cartoon.processor;

import com.lry.web.cartoon.entity.CartoonVariable;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.*;

public class CartoonVerifyProcessor implements PageProcessor {

    public CartoonVariable cartoonVariable;

    public  Map<String,Object> map;
    public CartoonVerifyProcessor() {
    }

    public CartoonVerifyProcessor(CartoonVariable cartoonVariable, Map<String,Object> map) {
        this.cartoonVariable = cartoonVariable;
        this.map = map;
    }

    private Site site = Site.me()
            .setSleepTime(1)//采集间隔时间
            .setTimeOut(1000 * 10)//超时时间
            .setRetrySleepTime(3000)//重试时间
            .setRetryTimes(3);//重试次数

    public Site getSite() {
        return site;
    }

    //解析页面
    public void process(Page page) {
        try{
        String url = page.getUrl().toString();
        String[] titless = cartoonVariable.getTitle().split("-!-");
        List<String> titlee = page.getHtml().xpath(titless[0]).regex(titless.length !=1 ? titless[1] : ".+").all();
        String[] contentss = cartoonVariable.getContent().split("-!-");
        List<String> contentt = page.getHtml().xpath(contentss[0]).regex(contentss.length !=1 ? contentss[1] : ".+").all();
        String[] cartoontitlelss = cartoonVariable.getCartoonTitle().split("-!-");
        List<String> cartoontitlell = page.getHtml().xpath(cartoontitlelss[0]).regex(cartoontitlelss.length !=1 ? cartoontitlelss[1] : ".+").all();
        //漫画搜索
        if (cartoontitlell.size()!=0) {
            String[] cartoonUrls = cartoonVariable.getCartoonUrl().split("-!-");
            String[] cartoontitlels = cartoonVariable.getCartoonTitle().split("-!-");
            List<String> cartoontitlel = page.getHtml().xpath(cartoontitlels[0]).regex(cartoontitlels.length !=1 ? cartoontitlels[1] : ".+").all();
            List<String> cartoonUrl = page.getHtml().xpath(cartoonUrls[0]).regex(cartoonUrls.length !=1 ? cartoonUrls[1] : ".+").all();
            //通过漫画标签判断搜索内容是否存在
            if (cartoonUrl.size() != 0) {
                String[] pageUrls = cartoonVariable.getPage().split("-!-");
                String pageUrl = page.getHtml().xpath(pageUrls[0]).regex(pageUrls.length !=1 ? pageUrls[1] : ".+").get();

                String fUrl = cartoonUrl.get(0);
                if(!cartoonUrl.get(0).contains("http")){
                    fUrl = cartoonVariable.getProtocol()+"://"+cartoonVariable.getUrl()+cartoonUrl.get(0);
                }
                if(pageUrl!=null) {
                    if (pageUrl.contains("http")) {
                    } else if (pageUrl.contains("/")) {
                        pageUrl = cartoonVariable.getProtocol() + "://" + cartoonVariable.getUrl() + pageUrl;
                    } else if (!pageUrl.contains("javascript")) {
                        pageUrl = url + pageUrl;
                    }
                }
                map.put("cartoonTitle",cartoontitlel.get(0));
                map.put("cartoonUrl",cartoonUrl.get(0));
                map.put("pageUrl",pageUrl);
                page.addTargetRequest(fUrl);
            }
        }else if(titlee.size()!=0){
            //获取漫画信息：title，desc,author,cartoonType,cover,CartoonUrl,CartoonId
            String[] titles = cartoonVariable.getTitle().split("-!-");
            String[] descs = cartoonVariable.getDescs().split("-!-");
            String[] authors = cartoonVariable.getAuthor().split("-!-");
            String[] cartoonTypes = cartoonVariable.getCartoonType().split("-!-");
            String[] covers = cartoonVariable.getCover().split("-!-");
            String[] chapterTitles = cartoonVariable.getChapterTitle().split("-!-");
            String[] chapterUrls = cartoonVariable.getChapterUrl().split("-!-");
            String[] updateTimes = cartoonVariable.getUpdateTime().split("-!-");
            List<String> title = page.getHtml().xpath(titles[0]).regex(titles.length !=1 ? titles[1] : ".+").all();
            List<String> desc = page.getHtml().xpath(descs[0]).regex(descs.length !=1 ? descs[1] : ".+").all();
            List<String> author = page.getHtml().xpath(authors[0]).regex(authors.length !=1 ? authors[1] : ".+").all();
            List<String> cartoonType = page.getHtml().xpath(cartoonTypes[0]).regex(cartoonTypes.length !=1 ? cartoonTypes[1] : ".+").all();
            List<String> cover = page.getHtml().xpath(covers[0]).regex(covers.length !=1 ? covers[1] : ".+").all();
            List<String> chapterTitle = page.getHtml().xpath(chapterTitles[0]).regex(chapterTitles.length !=1 ? chapterTitles[1] : ".+").all();
            List<String> chapterUrl = page.getHtml().xpath(chapterUrls[0]).regex(chapterUrls.length !=1 ? chapterUrls[1] : ".+").all();
            List<String> updateTime = page.getHtml().xpath(updateTimes[0]).regex(updateTimes.length !=1 ? updateTimes[1] : ".+").all();
            if(chapterTitles.length ==3){
                String[] splitTitl = chapterTitles[2].split(",");
                if(splitTitl.length ==1){
                    chapterTitle = chapterTitle.subList(Integer.parseInt(splitTitl[0]),chapterTitle.size());
                }else{
                    chapterTitle = chapterTitle.subList(Integer.parseInt(splitTitl[0]),Integer.parseInt(splitTitl[1]));

                }
            }
            if(chapterUrls.length == 3){
                String[] splitUrls = chapterUrls[2].split(",");
                if(splitUrls.length ==1){
                    chapterUrl = chapterUrl.subList(Integer.parseInt(splitUrls[0]),chapterUrl.size());
                }else{
                    chapterUrl = chapterUrl.subList(Integer.parseInt(splitUrls[0]),Integer.parseInt(splitUrls[1]));

                }

            }
            //判断是否存在
            if (title.size() == 0) {
                title.add(" ");
            }
            if (author.size() == 0) {
                author.add(" ");
            }
            if (cartoonType.size() == 0) {
                cartoonType.add(" ");
            }
            if (cover.size() == 0) {
                cover.add(" ");
            }
            if (updateTime.size() == 0) {
                updateTime.add(" ");
            }
            if (desc.size() == 0) {
                desc.add(" ");
            }
            if (chapterTitle.size() == 0) {
                chapterTitle.add(" ");
            }
            if (chapterUrl.size() == 0) {
                chapterUrl.add(" ");
            }
            if(!cover.get(0).contains("http")){
                cover.set(0,cartoonVariable.getProtocol() + "://" + cartoonVariable.getUrl() + cover.get(0));
            }
            String[] st =  chapterUrl.get(0).split("/");
            String chapterId = (st[st.length-1].replace(".html",""));
            String cartoonId = st[st.length-2].replace(".html","");
            map.put("title",title.get(0));
            map.put("cartoonType",cartoonType.get(0));
            map.put("author",author.get(0));
            map.put("descs",desc.get(0));
            map.put("cover",cover.get(0));
            map.put("updateTime",updateTime.get(0));
            map.put("chapterTitle",chapterTitle.get(0));
            map.put("chapterUrl",chapterUrl.get(0));
            map.put("chapterId",chapterId);
            map.put("cartoonId",cartoonId);
            String cUrl = chapterUrl.get(0);
            if(!chapterUrl.get(0).contains("http")){
                cUrl = cartoonVariable.getProtocol()+"://"+cartoonVariable.getUrl()+chapterUrl.get(0);
            }
            page.addTargetRequest(cUrl);

        }else if(contentt.size()!=0){
            //获取漫画内容信息：content

            String[] contents = cartoonVariable.getContent().split("-!-");

            List<String> content = page.getHtml().xpath(contents[0]).regex(contents.length !=1 ? contents[1] : ".+").all();

            if(cartoonVariable.getContentPage().length() !=0){
                List<String> list= new ArrayList<>();
                Spider.create(new CartoonContentProcessor(cartoonVariable,list))
                        //设置爬取数据的地址
                        .addUrl(url)
                        //设置多线程
                        .thread(5)
                        //执行爬虫
                        .run();
                content=list;
            }
            map.put("content",content);
            //漫画章节
        }
    }catch (Exception e) {
            System.out.print(e);
            map.put("Exception:",e);
        }
    }
}

